#include <stdio.h>
#include <stdlib.h>
#include "Structs.h"
#include "Pong3D.h"
#include <GL/glut.h>
#include <math.h>
#include <Windows.h>

void initballs(){
	int i;
	GLfloat startPos[3]  = {0,0,0}; 
	for (i = 0; i < MAX_BALLS; i++){
		copyArray(_balls[i].color, COLOR_YELLOW, 4);
		_balls[i].radius = 0.5f;
		_balls[i].speed = 1.0f;
		_balls[i].angleX = _balls[i].angleX =_balls[i].angleX = 0.0f;
		_balls[i].textureName = raw_texture_load("balls/eyeBall.tga", 300, 400);
		_balls[i].inPlay = false;
		_balls[i].vector[0] = floatRand(0.3);
		_balls[i].vector[1] = floatRand(0.3);
		_balls[i].vector[2] = 0.2f + floatRand(0.1);
		copyArray(_balls[i].vector, paddleDeltaVec, 3);
	}

}

void drawBalls(void){
	int i;
	GLUquadricObj *sphere = gluNewQuadric();
	GLfloat mat_emmision[] = {0.0, 1.0, 0.0, 1.0};
	GLfloat angleScale = 30.0f;
	if (__debug){
		printf("Balls in play: %i\n", _ballsInPlay);
	}
	for(i = 0; i < MAX_BALLS && _ballsInPlay > 0; i++){
		if (_balls[i].inPlay == true){
			glPushMatrix();
			glColor4fv(_balls[i].color);
			_balls[i].angleX = _balls[i].angleX + angleScale*_balls[i].vector[0];
			_balls[i].angleY = _balls[i].angleY + angleScale*_balls[i].vector[1];
			_balls[i].angleZ = _balls[i].angleZ + angleScale*_balls[i].vector[2];
			glTranslatef(_balls[i].position[0],
				_balls[i].position[1],
				_balls[i].position[2]);
			glRotatef(_balls[i].angleX,1,0,0);
			glRotatef(_balls[i].angleY,0,1,0);
			glRotatef(_balls[i].angleZ,0,0,1);

			//printf("Rotate x: %f  Rotate y: %f  Rotate z: %f \n", _balls[i].angleX, _balls[i].angleY, _balls[i].angleZ);
			drawBall(sphere, _balls[i].radius, _balls[i].textureName);
			glTranslatef(-1*_balls[i].position[0],
				-1*_balls[i].position[1],
				-1*_balls[i].position[2]);

			glPopMatrix();

		}
	}

}



void drawBall(GLUquadricObj *sphere, GLfloat radius, GLuint textureName){
	glColor4f(1,1,1,1);
	gluQuadricDrawStyle(sphere, GLU_FILL);
	gluQuadricTexture(sphere, 1);

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textureName);
	gluSphere(sphere, radius, 50, 50);
	glDisable(GL_TEXTURE_2D);
}

void checkCollisions(){
	int i, j, k, collCheck;
	float tempX, tempY;
	float scaleFac = -1.05f;

	for(i = 0; i < MAX_BALLS; i++){

		if(_balls[i].inPlay == false)
		{
			continue;
		} 
		else if (fabs( _balls[i].position[0]) > WORLD_WIDTH / 2 - _balls[i].radius)
		{ //Collision with side walls
			_balls[i].vector[0] *= scaleFac;
			PlaySound(TEXT("sounds\\wallhit.wav"),NULL, SND_FILENAME | SND_ASYNC);
		}
		else if ( fabs(_balls[i].position[1]) > WORLD_HEIGHT / 2 - _balls[i].radius)
		{ //Collision with top/bottom
			_balls[i].vector[1] *= scaleFac;
			PlaySound(TEXT("sounds\\wallhit.wav"),NULL, SND_FILENAME | SND_ASYNC);
		}
		else if(_balls[i].position[2] >= NEAR_PLANE - _balls[i].radius)
		{
			//PLayer Paddle collision
			tempX = _players[0].paddle.position[0] - _balls[i].position[0];
			tempY = _players[0].paddle.position[1] - _balls[i].position[1];

			if((fabs(tempX) < paddleXRadius) && (fabs(tempY) < paddleYRadius))
			{
				_balls[i].vector[2] *= scaleFac;
				PlaySound(TEXT("sounds\\wallhit.wav"),NULL, SND_FILENAME | SND_ASYNC);
			}
			else
			{
				_balls[i].inPlay = false;
				_ballsInPlay--;
				if(_ballsInPlay <= 0)
				{
					_players[1].score++;
					if (_players[1].score < MAX_POINTS)
					{
						changeState(ROUND_OVER);
					}else
					{
						changeState(GAME_WIN);
					}
				}
			}
		}
		else if(_balls[i].position[2] <= FAR_PLANE + _balls[i].radius)
		{
			tempX = _players[1].paddle.position[0] - _balls[i].position[0];
			tempY = _players[1].paddle.position[1] - _balls[i].position[1];

			if((fabs(tempX) < paddleXRadius) && (fabs(tempY) < paddleYRadius))
			{
				_balls[i].vector[2] *= scaleFac;
				PlaySound(TEXT("sounds\\wallhit.wav"),NULL, SND_FILENAME | SND_ASYNC);
			}
			else
			{
				_balls[i].inPlay = false;
				_ballsInPlay--;
				if(_ballsInPlay <= 0)
				{
					_players[0].score++;
					if (_players[0].score < MAX_POINTS)
					{
						changeState(ROUND_OVER);
					}else
					{
						changeState(GAME_WIN);
					}
				}
			}
		}

		for(j = i+1; j < MAX_BALLS; j++)
		{
			if(_balls[j].inPlay == false)
			{
				continue;
			}
			
			collCheck = 0;
			for(k = 0; k < 3; k++)
			{
				if(fabs(_balls[i].position[k] - _balls[j].position[k]) < (_balls[i].radius + _balls[j].radius))
				{
					collCheck++;
				}
			}

			if(collCheck == 3)
			{
				//Switch ball position
				_balls[i].vector[0] *= scaleFac;
				_balls[i].vector[2] *= scaleFac;

				
				_balls[j].vector[0] *= scaleFac;
				_balls[j].vector[1] *= scaleFac;
				_balls[j].vector[2] *= scaleFac;
			}
		}

		_balls[i].position[0] += _balls[i].vector[0];
		_balls[i].position[1] += _balls[i].vector[1];
		_balls[i].position[2] += _balls[i].vector[2];
	}

	
}